package com.dmp.userLabel.factory;

import com.dmp.common.string.StringUtils;
import com.dmp.util.Constant;
import com.dmp.util.JedisReadUtils;
import com.dmp.util.JedisWriteUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by wangqiang on 2016/4/7.
 */
public class LabelRedisDao implements ILabelRedisDao {

    Logger logger = LoggerFactory.getLogger(LabelRedisDao.class);

    private static final String fileName = "greenet.properties";

    public LabelRedisDao() {
        setRedisDao(fileName);
    }

    public LabelRedisDao(String fileName) {
        setRedisDao(fileName);
    }

    @Override
    public void setRedisDao(String fileName) {
        JedisReadUtils.setJedisPool(fileName);
        JedisWriteUtils.setJedisPool(fileName);
    }

    
    @Override
	public String getCarLabelValue(String field) {
    	String key = Constant.LabelRedisKey.LABEL_CAR_KEY;
		return getRedis(key,field);
	}

    private void insertRedis(String key, String value, int cacheSeconds) {
        //字段不允许为空
        if (StringUtils.isBlank(key))
            return;
        JedisWriteUtils.set(key, value, cacheSeconds);
    }

    private void insertRedis(String key, String field, String value, int cacheSeconds) {
        //字段不允许为空
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field))
            return;
        JedisWriteUtils.setHashMap(key, field, value, cacheSeconds);
    }

    private void insertRedis(String key, String field, String value) {
        //字段不允许为空
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field))
            return;
        JedisWriteUtils.setHashMap(key, field, value);
    }

    private String getRedis(String key) {
        //字段不允许为空
        if (StringUtils.isBlank(key))
            return null;
        return JedisReadUtils.get(key);
    }

    private String getRedis(String key, String field) {
        //字段不允许为空
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field))
            return null;
        return JedisReadUtils.getStringForMap(key, field);
    }

    private Map<String, String> getMapRedis(String key) {
        //字段不允许为空
        if (StringUtils.isBlank(key))
            return null;
        return JedisReadUtils.getMap(key);
    }

    private boolean existsRedis(String key, String field) {
        //字段不允许为空
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field))
            return false;
        return JedisReadUtils.mapExists(key, field);
    }

    private boolean exists(String key) {
        if (StringUtils.isBlank(key))
            return false;
        return JedisReadUtils.exists(key);
    }

    private void hincrByRedis(String key, String field) {
        //字段不允许为空
        if (StringUtils.isBlank(key) || StringUtils.isBlank(field))
            return;
        JedisWriteUtils.hincrBy(key, field, 1);
    }

}
